package com.ruoyi.project.digital.service.impl;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.digital.domain.TUser;
import com.ruoyi.project.digital.domain.Task;
import com.ruoyi.project.digital.domain.enums.TaskTypeEnum;
import com.ruoyi.project.digital.domain.enums.UserTaskStatusEnum;
import com.ruoyi.project.digital.domain.vo.UserTaskVO;
import com.ruoyi.project.digital.service.ITUserService;
import com.ruoyi.project.digital.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.digital.mapper.UserTaskMapper;
import com.ruoyi.project.digital.domain.UserTask;
import com.ruoyi.project.digital.service.IUserTaskService;

/**
 * 用户任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-31
 */
@Service
public class UserTaskServiceImpl extends ServiceImpl<UserTaskMapper, UserTask> implements IUserTaskService
{
    @Autowired
    private IUserTaskService userTaskService;

    @Autowired
    private ITaskService taskService;

    @Autowired
    private ITUserService userService;

    /**
     * 查询用户任务
     *
     * @param id 用户任务ID
     * @return 用户任务
     */
    @Override
    public UserTask selectUserTaskById(Long id)
    {
        return baseMapper.selectUserTaskById(id);
    }

    /**
     * 查询用户任务列表
     *
     * @param userTask 用户任务
     * @return 用户任务
     */
    @Override
    public List<UserTask> selectUserTaskList(UserTask userTask)
    {
        return baseMapper.selectUserTaskList(userTask);
    }

    /**
     * 新增用户任务
     *
     * @param userTask 用户任务
     * @return 结果
     */
    @Override
    public int insertUserTask(UserTask userTask)
    {
        userTask.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertUserTask(userTask);
    }

    /**
     * 修改用户任务
     *
     * @param userTask 用户任务
     * @return 结果
     */
    @Override
    public int updateUserTask(UserTask userTask)
    {
        return baseMapper.updateUserTask(userTask);
    }

    /**
     * 批量删除用户任务
     *
     * @param ids 需要删除的用户任务ID
     * @return 结果
     */
    @Override
    public int deleteUserTaskByIds(Long[] ids)
    {
        return baseMapper.deleteUserTaskByIds(ids);
    }

    /**
     * 删除用户任务信息
     *
     * @param id 用户任务ID
     * @return 结果
     */
    @Override
    public int deleteUserTaskById(Long id)
    {
        return baseMapper.deleteUserTaskById(id);
    }

    @Override
    public void acceptTask(Long taskId, Long userId) {
        UserTask userTask=new UserTask();
        userTask.setGetTime(new Date());
        userTask.setStatus(UserTaskStatusEnum.ACCEPT.getValue().toString());
        userTask.setTaskId(taskId);
        userTask.setUserId(userId);
        save(userTask);
    }

    @Override
    public List<UserTaskVO> taskDay(Long userId,String type) {
//        QueryWrapper<UserTask> queryWrapper=new QueryWrapper<>();
//        queryWrapper.select("t.*");
//        queryWrapper.eq("t.user_id",userId);
//        queryWrapper.eq("t.task_type", TaskTypeEnum.DAY.getValue());
        return baseMapper.taskDay(userId,type);
    }

    @Override
    public List<UserTask> taskTimeLine(Long userId) {
//        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取明天的日期
        LocalDate tomorrow = today.plusDays(1);
        // 获取前天的日期
        LocalDate beforeYesterday = today.minusDays(2);

        QueryWrapper<UserTask> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("task_type","0");
        wrapper.between("create_time",beforeYesterday,tomorrow);
        List<UserTask> list = userTaskService.list(wrapper);
        UserTask userTask = new UserTask();
//        Date from = Date.from(Instant.from(tomorrow));
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = tomorrow.atStartOfDay(zoneId);
        Date from = Date.from(zdt.toInstant());



        userTask.setCreateTime(from);
        userTask.setStatus("0");
        list.add(userTask);
        List<UserTask> resultDate=coverList(list);

        return resultDate;
    }

    private List<UserTask> coverList(List<UserTask> list) {

        for(UserTask userTask:list){
            userTask.setTimes(DateUtils.coverDate(userTask.getCreateTime()));
        }
        return list;

    }

    /*@Override
    public List<UserTaskVO> taskGame(Long userId) {
        QueryWrapper<UserTask> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("t.*");
        queryWrapper.eq("t.user_id",userId);
        queryWrapper.eq("t.task_type", TaskTypeEnum.GAME.getValue());
        return baseMapper.taskDay(userId,TaskTypeEnum.DAY.getValue());
    }*/

    @Override
    public void finishTask(Long id, Long taskId) {
        UserTask userTask1 = userTaskService.selectUserTaskById(taskId);
        UserTask userTask = new UserTask();
        userTask.setId(taskId);
        userTask.setStatus("1");
        userTask.setGetTime(new Date());
        userTaskService.updateById(userTask);
        Task task = taskService.selectTaskById(userTask1.getTaskId());
        TUser user = userService.getById(id);
        TUser tUser = new TUser();
        tUser.setId(id);
        tUser.setResStamina(user.getStamina() + task.getGetValue());
        userService.updateById(tUser);
//        baseMapper.finishTask(id,taskId,UserTaskStatusEnum.FINISH.getValue());
    }
}
